//import CostItem from "../controller/ui/cost_items"

import { Skill } from "../class/skill"
import { GameBalance } from "../data/GameBalance"
import DD from "../manager/dynamic_data_manager"
import JsonManager from "../manager/json_manager"
import MainManager from "../manager/main_manager"
import PoolManager from "../manager/pool_manager"
import StorageManager from "../manager/storage_manager"
import UIManager from "../manager/ui_manager"
import { Emitter } from "../utils/emmiter"
import { MessageType } from "../utils/message"


const { ccclass, property } = cc._decorator

interface OkCalBack {
    (isOK: boolean, param?: {}): void
}

@ccclass
export default class PlayerDetailUIManager extends cc.Component {
    static instance: PlayerDetailUIManager = null

    @property(cc.Node)
    uiNode: cc.Node = null

    @property(cc.Node)
    maskNode: cc.Node = null

    @property(cc.Label)
    labelTitle: cc.Label = null

    @property(cc.Label)
    labelDesc: cc.Label = null

    @property(cc.Button)
    btn1: cc.Button = null

    @property(cc.Label)
    costLable: cc.Label = null


    @property(cc.Label)
    lable1: cc.Label = null

    @property(cc.Label)
    lable2: cc.Label = null

    @property(cc.Label)
    lable3: cc.Label = null

    @property(cc.Label)
    lable4: cc.Label = null

    @property(cc.Button)
    btn2: cc.Button = null

    @property(cc.Button)
    btn3: cc.Button = null

    @property(cc.Button)
    buttonConfirm: cc.Button = null

    @property(cc.Label)
    moneyLabel: cc.Label = null

    @property(cc.Label)
    ticketLabel: cc.Label = null


    curCallBack: OkCalBack = null
    curParam: any = null
    id: string = ''

    _isClick: boolean = false

    labels: cc.Label[] = []
    level: number = 1
    unlockedItems = [1, 2, 3]
    playerNameMap = { 1: '普攻哥', 2: '爆破哥', 3: '驾驶员', 4: '弹球哥' }
    playerSkillMap = { 1: '普攻增加30%伤害', 2: '爆炸增加30%伤害', 3: '汽车50%概率多发一个', 4: '弹球50%概率多发一个' }

    @property(cc.Node)
    content1: cc.Node = null; // ScrollView 的 Content 节点

    onLoad() {
        // console.log("PoolManager Onload");
        PlayerDetailUIManager.instance = this

        this.maskNode.on('click', this.hideUI, this);
        this.buttonConfirm.node.on("click", this.confirmUpgrade, this)
        // this.labels.push(this.lable3)
        // this.labels.push(this.lable4)
        if (this.uiNode.active) {
            this.uiNode.active = false
        }
    }



    showUI(title: string, desc: string, callback: OkCalBack = null, param: {} = null, isNormal: boolean = true, labels: string[] = null) {
        cc.log(title, desc, callback)

        this.id = title
        if (!this.uiNode.active) {
            this.uiNode.active = true
            //  UIAnimaManager.instance.showDialog(this.uiNode, this.maskNode)
        }

        this.curParam = param
        this.populateScrollView()
        this.refreshUI()
    }

    // 填充 ScrollView
    populateScrollView() {
        this.content1.removeAllChildren(); // 清空之前的内容
        let bonusData = Object.values(this.playerSkillMap)
        // PoolManager
        bonusData.forEach((data, index) => {
            let item = PoolManager.instance.createObjectByName('list_btn', this.content1)
            // 创建每一项

            const label = item.getComponentInChildren(cc.Label);
            if (label) {
                label.string = `${data}`;
            }
            // 设置节点名称，便于定位
            item.name = `Item-${index}`;
            if (this.unlockedItems.includes(index)) {

                item.getComponent(cc.Button).interactable = true
                item.on('click', () => {
                    StorageManager.instance.levelInfo.chractorId = index + 1
                    this.refreshUI()
                }, this);
            } else {
                item.getComponent(cc.Button).interactable = false
            }


            // this.content1.addChild(item); // 添加到 Content
        });
        this.content1.getComponent(cc.Layout).updateLayout()
    }



    clearLabels() {
        this.lable1.string = "";
        this.lable2.string = "";
        this.lable3.string = "";
        this.lable4.string = "";
    }

    refreshUI() {
        let id = StorageManager.instance.levelInfo.chractorId ? StorageManager.instance.levelInfo.chractorId : 1
        cc.log(id, this.playerSkillMap[id])
        let level = StorageManager.instance.skillLevel[id]
        this.labelTitle.string = this.playerNameMap[id] + ' Lv' + StorageManager.instance.characterLevel[id]
        this.labelDesc.string = this.playerSkillMap[id]
        this.costLable.string = StorageManager.instance.skillTicketCost[id] + '/' + StorageManager.instance.skillTicketCost[level];
        // 
        // this.level = level
        // this.labelTitle.string = JsonManager.instance.getDataByName('skill')[id].name + ' Lv' + StorageManager.instance.skillLevel[id]
        // this.moneyLabel.string = StorageManager.instance.skillMoneyCost[level] + '/' + StorageManager.instance.levelInfo.money;
        // this.ticketLabel.string = StorageManager.instance.remainSkillTicket[id] + '/' + StorageManager.instance.skillTicketCost[level];


    }

    getId() {
        return StorageManager.instance.levelInfo.chractorId ? StorageManager.instance.levelInfo.chractorId : 1
    }


    confirmUpgrade() {
        let level = this.level;
        UIManager.instance.showTip('升级成功')
        let id = this.getId()
        StorageManager.instance.characterLevel[id]++;
        this.refreshUI()
        return;
        let remianTick = StorageManager.instance.remainSkillTicket[this.id]
        cc.log("money", StorageManager.instance.skillMoneyCost[level], StorageManager.instance.levelInfo.money)
        cc.log("id", this.id, "剩余", remianTick, StorageManager.instance.skillTicketCost[this.level])

        if (StorageManager.instance.levelInfo.money >= StorageManager.instance.skillMoneyCost[level] &&
            remianTick >= StorageManager.instance.skillTicketCost[this.level]
        ) {
            StorageManager.instance.levelInfo.money -= StorageManager.instance.skillMoneyCost[level];
            StorageManager.instance.remainSkillTicket[this.id] -= StorageManager.instance.skillTicketCost[this.level]
            StorageManager.instance.skillLevel[this.id]++;
            StorageManager.instance.savePlayerData()
            UIManager.instance.showTip('升级成功')
            this.refreshUI()
        } else {
            UIManager.instance.showTip('资源不足')
            return false
        }


        // this.hideUI()
    }


    confirm() {

        this.hideUI()
    }
    hideUI() {
        this.uiNode.active = false
        cc.director.resume();
        //  AudioManager.instance.playAudio('ui_close')

    }
}
